package com.itbaizhan.hdfs2hbase;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @Auther: hwh
 * @Date: 2023-02-28 - 02 - 28 - 11:39
 * @Description: com.itbaizhan.hdfs2hbase
 * @version: 1.0
 */
public class Hdfs2HbaseReducer extends TableReducer<Text, IntWritable,Text> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //定变量sum，表示当前单词出现的总次数
        int sum = 0;
        //遍历values
        for(IntWritable value : values){
            sum += value.get();
        }
        //创建Put类的对象，单词左右rowkey
        Put put = new Put(key.toString().getBytes());
        //为put指定列
        put.addColumn("cf".getBytes(),
                "count".getBytes(),
                Bytes.toBytes(sum));
        //输出
        context.write(key,put);
    }
}
